Ext.define('Admin.view.role.group.RoleGroupViewController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.roleGroupViewController',

    onFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onFormSubmitButton: function(btn) {
        var form = btn.up('form').getForm(),
            record = form.getRecord(),
            roleId = form.findField("role").getValue();

        if (roleId == record.data.role) {
            roleId = record.data.roleId;
        }

        Ext.Ajax.request({
            url: 'role/updateUserRole',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                id: record.data.id,
                roleId: roleId,
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.code === 200) {
                    btn.up('window').close();
                    Ext.getCmp('roleGroupGrid').getStore().reload();
                } else {
                    Ext.Msg.alert('警告', json.message);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onClickRoleGroupEditButton: function(view, recIndex, cellIndex, item, e, record) {
        var win = Ext.create('widget.roleGroupFormWindow');
        win.down('form').getForm().loadRecord(record);
    },


    onClickRoleGroupGridResetButton: function() {
        this.lookupReference('roleGroupQuery').setValue('姓名')
        this.lookupReference('roleSearchQuery').setValue('');
        var store = Ext.getCmp('roleGroupGrid').getStore();

        Ext.apply(store.proxy.extraParams, {
            fullname: '',
            dept: '',
            job: '',
            role: ''
        });

        store.load({
            params: {
                start: 0,
                limit: 20,
                page: 1
            }
        });
    },

    onClickRoleGroupGridSearchButton: function() {
        var key = this.lookupReference('roleGroupQuery').getValue(),
            value = this.lookupReference('roleSearchQuery').getValue(),
            store = Ext.getCmp('roleGroupGrid').getStore();

        if (key == null) {
            return;
        }

        Ext.apply(store.proxy.extraParams, {
            fullname: '',
            dept: '',
            job: '',
            role: ''
        });

        if (key == 'fullname') {
            Ext.apply(store.proxy.extraParams, {
                fullname: value
            })
        }

        if (key == 'dept') {
            Ext.apply(store.proxy.extraParams, {
                dept: value
            })
        }

        if (key == 'job') {
            Ext.apply(store.proxy.extraParams, {
                job: value
            })
        }

        if (key == 'role') {
            Ext.apply(store.proxy.extraParams, {
                role: value
            })
        }

        store.load({
            params: {
                start: 0,
                limit: 20,
                page: 1
            }
        });
    }

});